#include <stdio.h>
#include <stdlib.h>
#include "double_link_list.h"
//创建空链表
LIST *list_create(void){
	LIST *list = malloc(sizeof(LIST));
	list -> head = NULL;
	list -> tail = NULL;
	return list;
	}
LIST_NODE *node_create(int data,LIST_NODE *prev,LIST_NODE *next){
	LIST_NODE *node = malloc(sizeof(LIST_NODE));
	node -> data = data;
	node -> prev = prev;
	node -> next = next;
	return node;
	}
LIST_NODE *node_destroy(LIST_NODE *node ,LIST_NODE **tmp_prev){
	LIST_NODE *next = node -> next;
	*tmp_prev = node -> prev;
	free(node);
	return next;
	}
//销毁节点
int list_erase(LIST *list,int pos){
	//准备工作，找到要删除的节点
	LIST_NODE *find = NULL;
	for(find = list -> head; find ;find = find -> next){
		if(!pos--){
			LIST_NODE *prev = NULL;
			LIST_NODE *next = node_destroy(find,&prev);
			//从链表中剔除当前节点
			//调整前驱节点的next
			//调节后继节点的prev
			if(prev == NULL){//要删除的节点为第一个
				list -> head = next;
				}else{//删除的节点不是第一个
					prev -> next = next;
				}
			if(next == NULL){//要删除的节点为最后一个
				list -> tail = prev;
				}else{//删除的节点不是最后一个
				next -> prev = prev;
					}
			}
		}
	return 0;
	}
//销毁链表
void list_destroy(LIST *list){
	
	}
//插入头部节点
//void list_front(LIST *list,int data);
//指定位置插入
int list_insert(LIST *list,int pos,int data){
	//准备工作，找到插入位置
	LIST_NODE *find = NULL;
	for(find = list -> head;find ; find = find -> next){
		if(!pos){//找到插入位置
			//申请空间并初始化
			LIST_NODE *node = node_create(data,find,find -> next);
			//调整前矩和后继
			find -> next = node;
			if(node -> next ){
				node -> next -> prev = node;
				}else{
				//调整tail
				list -> tail = 	NULL;
					}
			}
		pos--;
		}
	return 0;
	}
//后向遍历
void list_next_print(LIST *list){
	//需要关注head tail
	//新节点的 next prev
	//老的结尾的next
	LIST_NODE *node = list -> head;
	for(;node;node =  node -> next){
			printf("%d ",node -> data);
		}
	printf("\n");
	}
//前向遍历
void list_prev_print(LIST *list){
	LIST_NODE *node = list -> head;
	for(node = list -> tail;node;node =  node -> prev){
			printf("%d ",node -> data);
		}
	printf("\n");
	}
//插入尾部节点
void list_append(LIST *list,int data){
	//动态申请节点空间并初始化
	//重置了链表尾节点 （tail指针）
	list -> tail = node_create(data,list -> tail,NULL);
	//如果原来位空链表，修改head的值
	if(list -> head == NULL){
		list -> head = list -> tail;
		}
	//如果原链表不为空 
		else{
			//原尾节点的next= 新尾节点的地址
			list -> tail -> prev -> next = list -> tail;
			}
	}
